import 'package:flutter_base_package/app/data/api/api_connect.dart';
import 'package:get/get.dart';
import 'dart:convert' as convert;

import 'toast_utils.dart';

typedef Success<T> = Function(T result);
typedef Fail<String> = Function(String errorMsg);

class HttpUtils {
  static void _decoderData(Response response, {Success? success, Fail? fail}) {
    if (response.statusCode == 200) {
      dynamic data = response.getBody();
      if (data['code'] == "0") {
        if (success != null) {
          success(data);
        }
      } else if (data['code'] == 401) {
        // KVDataUtils.getInstance().removeKey("token");
        // AppData.instance.token = null;
        // Get.offAllNamed(AppRoutes.login)?.then((value) {
        //   ToastUtils.toast("登录失效，请重新登录");
        // });
      } else {
        if (fail != null) {
          fail("${data['msg']}");
          ToastUtils.toast("${data['msg']}");
        } else {
          ToastUtils.toast("${data['msg']}");
        }
      }
    } else {
      if (fail != null) {
        fail("${response.bodyString}");
        ToastUtils.toast(
            "response: ${response.bodyString} \n statusText: ${response.statusText}");
      } else {
        ToastUtils.toast(
            "response: ${response.bodyString} \n statusText: ${response.statusText}");
      }
    }
  }

  static Future<Response> get(
    String url, {
    Map<String, String>? headers,
    String? contentType,
    Map<String, dynamic>? query,
    Decoder? decoder,
    bool needToken = true,
    Success? success,
    Fail? fail,
  }) async {
    Response response = await ApiConnect.instance.get(
      url,
      headers: headers,
      contentType: contentType,
      query: query,
      decoder: decoder,
    );
    _decoderData(response, success: success, fail: fail);
    return response;
  }

  static Future<Response> delete(
    String url, {
    Map<String, String>? headers,
    String? contentType,
    Map<String, dynamic>? query,
    Decoder? decoder,
    bool needToken = true,
    Success? success,
    Fail? fail,
  }) async {
    Response response = await ApiConnect.instance.delete(
      url,
      headers: headers,
      contentType: contentType,
      query: query,
      decoder: decoder,
    );
    _decoderData(response, success: success, fail: fail);
    return response;
  }

  static Future<Response> post(
    String? url, {
    dynamic body,
    String? contentType,
    Map<String, String>? headers,
    Map<String, dynamic>? query,
    Decoder? decoder,
    bool needToken = true,
    Progress? uploadProgress,
    Success? success,
    Fail? fail,
  }) async {
    Response response = await ApiConnect.instance.post(
      url,
      convert.jsonEncode(body),
      headers: headers,
      contentType: contentType,
      query: query,
      decoder: decoder,
    );
    _decoderData(response, success: success, fail: fail);
    return response;
  }

  static Future<Response> put(
    String url,
    dynamic body, {
    String? contentType,
    Map<String, String>? headers,
    Map<String, dynamic>? query,
    Decoder? decoder,
    bool needToken = true,
    Progress? uploadProgress,
    Success? success,
    Fail? fail,
  }) async {
    Response response = await ApiConnect.instance.put(
      url,
      convert.jsonEncode(body),
      headers: headers,
      contentType: contentType,
      query: query,
      decoder: decoder,
    );
    _decoderData(response, success: success, fail: fail);
    return response;
  }
}
